package org.mch.func.sys.func;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.tree.DefaultTreeModel;

import org.mch.comm.db.utils.MDao;
import org.mch.form.tree.MTreeNode;

import com.mch.ar.core.Record;

/**
 * 描述：功能菜单树Model类；<BR/>
 * 
 * @author ZhangLi；mchweb.net；<BR/>
 *
 */
public class SysFuncTreeModel {

	private List<Record> listFuncTree;
	private DefaultTreeModel treeModel;

	public SysFuncTreeModel() {
		super();
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void init() throws Exception {
		queryFuncTreeData();

		buildTreeModel();
	}

	protected void buildTreeModel() {
		MTreeNode root = new MTreeNode(new Record().set("nodecode", ""), "菜单列表");

		Map<String, MTreeNode> map = new HashMap<String, MTreeNode>();
		map.put("", root);

		for (int i = 0, len = getListFuncTree().size(); i < len; i++) {
			Record eo = getListFuncTree().get(i);
			String upcode = eo.getStr("nodecode").substring(0, eo.getStr("nodecode").length() - 2);
			MTreeNode node = new MTreeNode(eo, eo.getStr("nodename"));

			map.get(upcode).add(node);

			map.put(eo.getStr("nodecode"), node);
		}

		this.treeModel = new DefaultTreeModel(root);
	}

	protected void queryFuncTreeData() throws Exception {
		this.listFuncTree = MDao.db.active("m_func").all("ordercode");
	}

	public DefaultTreeModel getTreeModel() {
		return treeModel;
	}

	public List<Record> getListFuncTree() {
		return listFuncTree;
	}
}
